package fr.axgame.utils;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;

import fr.axgame.Fight;

public class Simulator {

	private Fight fight;
	
	public Simulator(Fight fight){
		this.fight = fight;
	}
	
	public String simulate(){
		FightReport report = null;
		int runs = 1000000;
		int i = 0;
		int numberOfWins = 0;
		Map<Integer, Integer> numberOfRounds = new HashMap<Integer, Integer>();
		try {
			while (i < runs){
		        report = fight.simulateFight();
		        if (report.isWin()){
		        	numberOfWins++;
		        }
		        if (numberOfRounds.get(report.getRound()) != null){
		        	numberOfRounds.put(report.getRound(), numberOfRounds.get(report.getRound()) + 1);
		        } else {
		        	numberOfRounds.put(report.getRound(), 1);
		        }
		        fight.reinitFight();
		        i++;
			}
	    } catch (Exception e) {
	        e.printStackTrace();
	    }
		StringBuilder sb = new StringBuilder();
		sb.append("Run : " + i + "\n");
		sb.append("Win : " + numberOfWins + "\n");
		for (Integer r : numberOfRounds.keySet()){
			sb.append(r + " rounds : " + new BigDecimal(100 * numberOfRounds.get(r)).divide(new BigDecimal(runs), 2, BigDecimal.ROUND_HALF_DOWN) + " %\n"); 
		}
		return sb.toString();
	}
	
	public Fight getFight() {
		return fight;
	}

	public void setFight(Fight fight) {
		this.fight = fight;
	}
	
}
